// index.js
// 获取应用实例
const app = getApp()
import {
  getCaVenueRentalPage,
  getCaVeueRentalObjPage,
  getCaVenueRentalPhotoPage,
} from "../../../api/place.js";
import {
  insertCmComment
} from "../../../api/user.js";
import {
  getCmCommentPage,
  getCmCommentAppendPage
} from "../../../api/post.js";
Page({
  data: {
    index:0,
    num: '',
    camp: "",
    latitude: 29.539852, //纬度
    longitude: 106.565742, //经度
    makers: [{
      iconPath: '/images/定位.png',
      id: 0,
      latitude: 29.539852,
      longitude: 106.565742,
      width: 50,
      height: 50
    }],
    swiperList: [{
      id: 0,
      type: 'image',
      photoUrl: '/images/图片/2.jpg'
    }, ],
    photolist: [{
      id: 0,
      type: 'image',
      select: false,
      photoUrl: '/images/图片/4.jpg'
    }, ],
    imgDetail: [{
      photoUrl: '/images/图片/2.jpg'
    }],
    showtitle: false,
    showfuwu: false,
    visible: false,
    showIndex: false,
    closeBtn: false,
    deleteBtn: false,
    images: [],
    lostPrice: 500,
    total: "",
    type:1,
    id:1,
  },
  makertap: function () {
    let _this = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        // console.log(res);
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        _this.setData({
          latitude: latitude,
          longitude: longitude
        })
      }
    })
  },
  buttonTap: function () {
    let id =  this.data.photolist[this.data.index].id
    id = JSON.stringify(id)
    wx.navigateTo({
      url: '/pages/navigation/navigation?id='+id,
    })
  },
  onClick() {
    // this.getCaVeueRentalObjPage(id)
    this.setData({
      images: [
        this.data.photolist[this.data.index].photoUrl
      ],
      showIndex: true,
      visible: true,
      closeBtn: true,
      deleteBtn: true,
    });
  },
  onChange(e) {
    const {
      index
    } = e.detail;

    // console.log(index);
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

    go: function () {
      // console.log(this.data.photolist)
    let id =  this.data.photolist[this.data.index].id
    id = JSON.stringify(id)
    let type=this.data.type
    let data={
      id:id,
      type:type
    }
    data = JSON.stringify(data)
    wx.navigateTo({
      url: '../payment/payment?data='+data
    })
  },
  onLoad(options) {

    // let id = JSON.parse(options.id)
    let data = JSON.parse(options.data);
    this.data.id=data.id
    // console.log(data);
    this.getCaVenueRentalPage(data)
    this.makertap()
    this.getCaVenueRentalPhotoPage(data.id)
    this.getCaVeueRentalObjPage(data.id)
    this.getCmCommentPage(data.id)
  },
  getCmCommentPage(id) {
    let data = {
      "pageNum": "1",
      "pageSize": "10",
      "id": null,
      "uaUserId": null,
      "commentType": 1,
      "commentTypeId": id,
      "commentText": null,
      "praiseNum": null,
      "followNum": null,
      "status": null,
      "delFlag": null,
      "createBy": null,
      "createTime": null,
      "updateBy": null,
      "updateTime": null,
      "remark": null
    }
    getCmCommentPage(data).then(res => {
      let datalist = res.data.data.records;
      
      // console.log(datalist)
      this.setData({
        datalist: datalist,
        num: datalist.length,
        
      })
      datalist.forEach((item,index)=>{
        this.getCmCommentAppendPage(item.id,index)
      })
    })
    
  },
  getCmCommentAppendPage(id,index) {
    let data = {
      "pageNum": "1",
      "pageSize": "10",
      "id": null,
      "cmCommentId":id,
      "uaUserId": null,
      "commentText": null,
      "praiseNum": null,
      "status": null,
      "delFlag": null,
      "createBy": null,
      "createTime": null,
      "updateBy": null,
      "updateTime": null,
      "remark": null
    }
    getCmCommentAppendPage(data).then(res => {
      let datalist1 = res.data.data.records;
      console.log(datalist1)
      let total = res.data.data.total

      let datalist = this.data.datalist

      datalist[index].datalist1 = datalist1
      datalist[index].total = total

      this.setData({
        datalist: datalist,
      })
    })
  },
  getCaVenueRentalPhotoPage(id) {
    let data = {
      "pageNum": "1",
      "pageSize": "10",
      "id": null,
      "caVenueRentalId": id,
      "photoUrl": null,
      "status": null,
      "delFlag": null,
      "createBy": null,
      "createTime": null,
      "updateBy": null,
      "updateTime": null,
      "remark": null
    }
    getCaVenueRentalPhotoPage(data).then(res => {
      let datalist = res.data.data.records
      this.setData({
        swiperList: datalist,
        imgDetail: datalist
      })
    })
  },
  getCaVenueRentalPage(option) {
    let data = {
      "pageNum": "1",
      "pageSize": "10",
      "id": option.id,
      "caCampId": option.campid,
      "freight": null,
      "service": null,
      "title": null,
      "lostPrice": null,
      "detailText": null,
      "photoUrl": null,
      "praiseNum": null,
      "collectionNum": null,
      "status": null,
      "delFlag": null,
      "createBy": null,
      "createTime": null,
      "updateBy": null,
      "updateTime": null,
      "remark": null
    }
    getCaVenueRentalPage(data).then(res => {
      let photolist = res.data.data.records;
      let camp = res.data.data.records[0].caCamp;
      camp.latitude = '29.678534618042985'
      camp.longitude = '116.01511473945618'
      // console.log(photolist);
      this.setData({
        camp: camp,
        photolist: photolist,
      })
    })
  },
  changecolor(e) {
    let index = (Number)(e.target.dataset.index);
    let list = this.data.photolist;
    let old = list[index].select;
    list.forEach(item => {
      item.select = false
    })
    list[index].select = !old;
    this.setData({
      photolist: list
    })
  },
  //获取对象
  getCaVeueRentalObjPage(id) {
    let data = {
      "pageNum": "1",
      "pageSize": "10",
      "id": null,
      "caVenueRentalId": id,
      "veueRentalCode": null,
      "photoUrl": null,
      "price": null,
      "inventory": null,
      "transactionNum": null,
      "status": null,
      "delFlag": null,
      "createBy": null,
      "createTime": null,
      "updateBy": null,
      "updateTime": null,
      "remark": null
    }
    getCaVeueRentalObjPage(data).then(res => {
      let datalist = res.data.data.records;
      let num = datalist.length;
      console.log(datalist)
      this.setData({
        photolist: datalist,
        num: num
      })
    })
  },
  onClose() {
    this.setData({
      showtitle: false,
      showfuwu: false
    });
  },
  onClose1(e) {
    const {
      trigger
    } = e.detail;
    this.setData({
      visible: false,
    });
  },
  jumpPage() {

  },
  showtitle() {
    this.getCaVeueRentalObjPage(this.data.id);
    this.setData({
      showtitle: true
    });
  },
  showfuwu() {
    this.setData({
      showfuwu: true
    });
  },
  getprice(e) {
    let index = e.currentTarget.dataset.index;
    console.log(e);
    this.setData({
      index:index,
    })
  },
})